{*
SPDX-FileCopyrightText: © 2021 Olivier Meunier <olivier@neokraft.net>

SPDX-License-Identifier: AGPL-3.0-only
*}
{{- import "/_libs/common" -}}
{{- textArch := .IsArchived ? "Remove from archive" : "Move to archive" -}}
{{- textFav := .IsMarked ? "Remove from favorites" : "Add to favorites" -}}

<turbo-frame id="bookmark-actions-{{ .ID }}">
<div class="flex my-4 items-center gap-2 print:hidden">
  {{- if .IsDeleted -}}
    <span class="text-red-700" data-deleted="true">
      This bookmark will be removed in a few seconds.
    </span>
    <form action="{{ urlFor(`/bookmarks`, .ID, `delete`) }}" method="post">
      {{ yield csrfField() }}
      <input type="hidden" name="cancel" value="1" />
      <input type="hidden" name="_to" value="{{ currentPath }}" />
      <button class="btn-primary ml-2" type="submit" name="is_deleted" value="0"
       data-controller="turbo-form"
       data-turbo-form-action-value="{{ urlFor(`/api/bookmarks`, .ID) }}"
       data-turbo-form-method-value="patch">{{ yield icon(name="o-undo") }}&nbsp;Undo</button>
    </form>
  {{- else -}}
    <form action="{{ urlFor(`/bookmarks`, .ID) }}" method="post"
    data-controller="turbo-form"
    data-turbo-form-action-value="{{ urlFor(`/api/bookmarks`, .ID) }}"
    data-turbo-form-method-value="patch">
      {{ yield csrfField() }}
      <div class="inline-flex relative">
        <button class="btn-outlined-primary leading-none rounded-l-full border-r-0 h-9 pl-3" name="is_marked" value="{{ .IsMarked ? 0 : 1 }}"
        title="{{ textFav }}">
          {{ yield icon(name=(.IsMarked ? "o-favorite-on" : "o-favorite-off")) }}
        </button>
        <button class="btn-outlined-primary leading-none border-x-0 h-9" name="is_archived" value="{{ .IsArchived ? 0 : 1 }}"
        title="{{ textArch }}">
          {{ yield icon(name=(.IsArchived ? "o-archive-on" : "o-archive-off")) }}
        </button>
        <details class="" data-controller="details">
          <summary class="btn-outlined-primary leading-none border-l-0 rounded-r-full h-9 pr-3"
           title="Export and Share">
            {{ yield icon(name="o-share") }}
          </summary>
          <div class="absolute z-30 top-10 left-0 w-[max-content] shadow-lg bg-gray-50 border border-gray-300 rounded-md p-4">
            <ul class="flex flex-col gap-2 space-y-2">
              <li><a class="link" href="{{ urlFor(`/api/bookmarks`, .ID, `article.epub`) }}">{{ yield icon(name="o-download") }} Download EPUB</a></li>
              {{ if hasPermission("api:bookmarks", "export") -}}
              <li><button class="link" form="share-form" data-details-target="closer">{{ yield icon(name="o-link") }} Share by Link</button></li>
              {{- end }}
            </ul>
          </div>
        </details>
      </div>
    </form>

    <form action="{{ urlFor(`/bookmarks`, .ID, `delete`) }}" method="post"
     class="ml-auto">
      {{ yield csrfField() }}
      <input type="hidden" name="_to" value="{{ currentPath }}" />
      <button class="btn-outlined-danger leading-none rounded-full w-9 h-9 px-2" name="is_deleted" value="1"
        data-controller="turbo-form"
        data-turbo-form-action-value="{{ urlFor(`/api/bookmarks`, .ID) }}"
        data-turbo-form-method-value="patch">
        {{ yield icon(name="o-trash") }}
      </button>
    </form>
  {{- end -}}
</div>
</turbo-frame>
